package com.wzw.basics.sort;

import java.util.Arrays;

/**
 * 希尔排序
 *
 * @author Wangzhiwen
 */
public class ShellSort {

    public static void execute(int[] arrays) {
        if (arrays == null || arrays.length == 0) {
            return;
        }
        int length = arrays.length;
        int temp, gap = length / 2;
        while (gap > 0) {
            for (int i = gap; i < length; i++) {
                temp = arrays[i];
                int preIndex = i - gap;
                while (preIndex >= 0 && arrays[preIndex] > temp) {
                    arrays[preIndex + gap] = arrays[preIndex];
                    preIndex -= gap;
                }
                arrays[preIndex + gap] = temp;
            }
            gap /= 2;
        }
        System.out.println("shell sort " + Arrays.toString(arrays));
    }

}
